package yanc.app

import grails.transaction.Transactional
import org.springframework.beans.factory.annotation.Value

@Transactional
class BusinessService {
    @Value('${businessService.urll}')
    protected String PATH

    @Value('${commService.urls}')
    protected String URLS

    def add(Business branch, String title, String imgUrl) {
        branch.id = new Date().format("yyyyMMddHHmmss")
        println branch.id
        branch.url = PATH + branch.id
        branch.createDate = new Date()
        branch.modifyDate = new Date()
        branch.imgUrl = URLS + imgUrl
        def comm = News.findById(title)
        branch.title = comm.title
        branch.comm = comm
        branch.insert()
    }

    def update(Business business, String title, String imgUrl) {
        if (business.imgUrl != imgUrl) {
            println "update"
            business.imgUrl = URLS + imgUrl
        }
        business.modifyDate = new Date()
        if (business.title != title) {
            def comm = News.findById(title)
            business.title = comm.title
            business.comm = comm
        }
        business.save()
        if (!business.save()) {
            business.errors.allErrors.each {
                println it
            }
        }
    }

    def del(String id) {
        def business = Business.findById(id)
        business.delete()
    }
}
